#include "stc.h"
#define SIZE 1000

int correct[SIZE] = {0};
int check[1000] = {0};

void create_arr() {
	int i = 0;
	while (i < 1000) {
		if(i <= 10) check[i]=-1;														 
		else if(10<i && i<1000 && (i%10== i/10%10 || i%10==i/100%10 || i/10%10== i/100%10)) check[i]=-1;
		else check[i]= 0; 			
		i = i +1;
	}
}

int play_game(int r1, int r2, int r3) {
	int i = 0;
	int max = 0;
	int try_ = 0;
	while (i < 1000) {
		if (check [i] == max) {
			max = check[i] + 1;
			try_ = try_ + 1;
			int a1 = i/100%10;
			int a2 = i/10%10;
			int a3 = i%10;

			PutStr("try ");
			PutInt(try_);
			PutStr(" : ");
			PutInt(a1);
			PutStr(" ");
			PutInt(a2);
			PutStr(" ");
			PutInt(a3);
			PutStr("\n");
			int strike_ball[2] = {0};
			strike_ball[0] = (a1 == r1) + (a2 == r2) + (a3 == r3);
			strike_ball[1] = (a1 == r2 || a1 == r3) + (a2 == r1 || a2 == r3) + (a3 == r1 || a3 == r2);
			PutStr("strike : ");
			PutInt(strike_ball[0]);
			PutStr(",\t");
			PutStr("ball : ");
			PutInt(strike_ball[1]);
			PutStr("\n");
			if (strike_ball[0] == 3) break;
			else {
				int j = i+1;
				while (j < 1000) {
					int b1 = j/100%10;
					int b2 = j/10%10;
					int b3 = j%10;

					int strike = (a1 == b1) + (a2 == b2) + (a3 == b3);
					int ball = (a1 == b2 || a1 == b3) + (a2 == b1 || a2 == b3) + (a3 == b1 || a3 == b2);

					if (check[j] == max -1 && strike == strike_ball[0] && ball == strike_ball[1]) {
						check[j] = max;
					}

					j = j +1;
				}
			}
		}
		i = i +1;
	}

	PutStr("Success !!! count : ");
	PutInt(try_);
	return try_;
}

void main() {
	int i = 1;
	int total = 0;
	while(i <= SIZE) {
		create_arr();

		int r1 = 0;
		int r2 = 0;
		int r3 = 0;
		r1  = RandNum();
		r1= r1%10 ;
		while(r2==r1)
		{  
			r2  = RandNum();
			r2= r2%10 ;
		}
		while (r3==r2|| r3==r1) 
		{
			r3  = RandNum();
			r3= r3%10 ;	 
		}
		PutStr("\nthree number : ");
		PutInt(r1);
		PutStr(" ");
		PutInt(r2);
		PutStr(" ");
		PutInt(r3);
		PutStr("\n");
		correct[i] = play_game(r1,r2,r3);
		total = total + correct[i];
		PutStr("\n----------");
		PutInt(i);
		PutStr("-----------");
		i = i+1;
	}

	PutStr("\naverage : ");
	PutInt(total/SIZE);
	PutStr("\npress any key to continue .........");
	GetChar();
}

